package view;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import orm.MyBatisSQLSession;
import orm.Word;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class WordItem extends JPanel {
    int id;
    JTextField word;
    JTextField meaning;
    JTextField sentence;
    JTextField audio;
    JButton pronounceButton = new JButton();
    JButton updateButton = new JButton();
    JButton deleteButton = new JButton();
    JCheckBox favoriteCheckBox;

    public WordItem(int id, String word, String meaning, String sentence, String audio, boolean favorite) {
        this.id = id;
        this.word = new JTextField(word, 12);
        this.word.setToolTipText("英文单词");
        this.word.setFont(MainView.font);
        this.meaning = new JTextField(meaning, 12);
        this.meaning.setToolTipText("中文释义");
        this.meaning.setFont(MainView.font);
        this.favoriteCheckBox = new JCheckBox("收藏", favorite);
        this.favoriteCheckBox.setFont(MainView.font);
        this.pronounceButton.setIcon(new ImageIcon("icons/voice.png"));
        this.pronounceButton.setToolTipText("发音");
        this.updateButton.setIcon(new ImageIcon("icons/save.png"));
        this.updateButton.setToolTipText("更新单词");
        this.deleteButton.setIcon(new ImageIcon("icons/delete.png"));
        this.deleteButton.setToolTipText("删除单词");
        this.sentence = new JTextField(sentence, 42);
        this.sentence.setToolTipText("例句");
        this.audio = new JTextField(audio, 10);
        this.audio.setToolTipText("音频文件");

        setListener();

        Box box = Box.createVerticalBox();
        box.setBorder(BorderFactory.createTitledBorder(""));
//        box.add(Box.createHorizontalStrut(10));
        JPanel panelAbove = new JPanel();
        JPanel panelBelow = new JPanel();
        panelAbove.add(this.word);
        panelAbove.add(this.meaning);
        panelAbove.add(this.favoriteCheckBox);
        panelAbove.add(this.pronounceButton);
        panelAbove.add(this.updateButton);
        panelAbove.add(this.deleteButton);
        panelBelow.add(this.sentence);
        panelBelow.add(this.audio);
        box.add(panelAbove);
        box.add(panelBelow);
        add(box);
    }

    void setListener() {
        this.pronounceButton.addActionListener(actionEvent -> {
            class Sleep implements Runnable {
                @Override
                public void run() {
                    try {
                        File file = new File(audio.getText());
                        FileInputStream stream = new FileInputStream(file);
                        Player player = new Player(stream);
                        player.play();
                    } catch (FileNotFoundException | JavaLayerException e) {
                        MainViewAction.showHint("错误的音频文件");
                        e.printStackTrace();
                    }
                }
            }
            new Thread(new Sleep()).start();
        });

        this.updateButton.addActionListener(actionEvent -> {
            Word word1 = getWord();
            MyBatisSQLSession.getWordMapper().update(word1);
            MainViewAction.showHint("已更新 " + word.getText());
        });

        this.deleteButton.addActionListener(actionEvent -> {
            MyBatisSQLSession.getWordMapper().delete(id);
            MainViewAction.showHint("已删除 " + word.getText());
            MainViewAction.refreshWords();
        });

        this.favoriteCheckBox.addItemListener(itemEvent -> {
            Word word1 = getWord();
            MyBatisSQLSession.getWordMapper().setFavorite(word1);
            String s;
            if (favoriteCheckBox.isSelected()) {
                s = "已收藏 ";
            } else {
                s = "已取消收藏 ";
            }
            MainViewAction.showHint(s + word.getText());
        });
    }

    private Word getWord() {
        return new Word(
                id,
                word.getText(),
                meaning.getText(),
                sentence.getText(),
                audio.getText(),
                favoriteCheckBox.isSelected(),
                MainView.userId
        );
    }
}
